﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;

namespace Persistente.Newsletter
{
    public class PNewsletter
    {
        private DB_SIMETRIA _db;
        private SqlConnection _conexao = new SqlConnection(ConfigurationManager.ConnectionStrings["DB_SIMETRIA_SQL"].ConnectionString);
        public bool Salvar(string teEmail)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var tbNewsletter = new tb_newsletter();
                tbNewsletter.te_email = teEmail;
                tbNewsletter.dt_cadastro = DateTime.Now;

                _db.AddTotb_newsletter(tbNewsletter);
                _db.SaveChanges();

                return true;

            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }

        }

        public bool Remover(int idNewsletter)
        {
            try
            {
                _db = new DB_SIMETRIA();
                var obTbNewsletter = _db.tb_newsletter.Where(q => q.id_newsletter == idNewsletter).FirstOrDefault();
                if (obTbNewsletter == null)
                {
                    return false;
                }
                else
                {
                    _db.tb_newsletter.DeleteObject(obTbNewsletter);
                    _db.SaveChanges();
                    return true;
                }

            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

        public tb_newsletter pegarNewsletterPorId(int idNewsletter)
        {
            try
            {
                _db = new DB_SIMETRIA();
                return _db.tb_newsletter.Where(q => q.id_newsletter == idNewsletter).FirstOrDefault();
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                _db.Connection.Close();
            }
        }

    }
}
